import {
  FormatDateToQuarter,
  GetISOWeeksNumber,
  GetLastDayOfMonthFormatted,
  getOptionValueFromTable,
  GetWeekRange,
  HearderType,
  SplitWeekString,
} from "@/common/common";
import { httpClient } from "@/common/httpClient";
type keysType = {
  label: string;
  value: string;
};
export const keys: keysType[] = [
  {
    label: "需要读取的寄存器值",
    value: "readRegister",
  },
  {
    label: "需要写入的寄存器值",
    value: "writeRegister",
  },
  {
    label: "统计寄存器",
    value: "totalRegister",
  },
  {
    label: "控制类寄存器",
    value: "controlRegister",
  },
];
//递归添加数据
export const addDataTree = async (arr: any = [], resLabels: any[] = []) => {
  // debugger;
  arr.forEach((arrv: any) => {
    let resLabelArray = arrv.selectValues.map((v: any) => {
      let resLabel = resLabels.find((v1: any) => v1.id == v);
      return {
        registerId: resLabel.id,
        label: resLabel.label,
        value: resLabel.value,
        registerNum: resLabel.registerNum,
        extendsJson: JSON.parse(resLabel.extendsJson || "{}"),
      };
    });
    arrv.resLabelArray = resLabelArray;
    if (arrv.child) {
      addDataTree(arrv.child, resLabels);
    }
  });
};
export const getGroupDetail = async (groupId: number) => {
  return httpClient.get(
    "/admin/device_group/detail",
    { groupId },
    {
      headers: { "Content-Type": HearderType[0] },
    }
  );
};
export const getGroupExtensRegister = async (groupId: number) => {
  let resLabels = await getOptionValueFromTable(
    "device_register",
    {
      groupId,
    },
    ["registerOffset", "registerName", "extendsJson", "id", "registerNum"]
  );
  let res = await httpClient.get(
    "/admin/device_group/detail",
    { groupId },
    {
      headers: { "Content-Type": HearderType[0] },
    }
  );
  let obj: Record<string, any> = {};
  // let selectValus: any[] = [];
  keys.forEach((v: keysType) => {
    let arr = JSON.parse((res || {})[v.value] || "[]");
    addDataTree(arr, resLabels);

    obj[v.value] = arr;
  });
  return obj; // 添加返回语句
};
export const getDeviceRegisterRecord = async (
  registerOffset: string,
  device_Id: number,
  param: object = {}
) => {
  let res = await httpClient.get(
    "/admin/device_register_record/lists",
    { registerOffset, deviceId: device_Id, ...param },
    {
      headers: { "Content-Type": HearderType[0] },
    }
  );
  return res.items;
};
export const getDeviceRegisterRecordFromDay = async (
  dateType: string,
  dates: string | string[],
  registerOffset: string,
  device_Id: number
) => {
  let param: Record<string, any> = {};
  if (dateType == "week") {
    dateType = "day";
    if (!dates.includes("th")) {
      dates = GetISOWeeksNumber(dates as string);
    }
    let { start, end } = SplitWeekString(dates as string);
    dates = [start, end];
    param.dates = JSON.stringify(dates);
  }
  if (dateType == "month") {
    dateType = "day";
    let end = GetLastDayOfMonthFormatted(dates as string);
    dates = [dates as string, end];
    param.dates = JSON.stringify(dates);
  }
  if (dateType == "year") {
    param.dates = new Date(dates as string).getFullYear();
    dateType = "month";
  }

  if (dateType == "quarter") {
    param.year = new Date(dates as string).getFullYear();
  }

  let res = await httpClient.get(
    "/admin/device_total_record_" + dateType + "/lists",
    {
      // registerId: JSON.stringify(registerOffset),
      deviceId: device_Id,
      ...param,
    },
    {
      headers: { "Content-Type": HearderType[0] },
    }
  );
  return res.items || [];
};
